package groups.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;


public class DBDumper {
	private String driver;
	private String connectionString;
	private String user;
	private String password;
	private Vector<String> sqlPaths;
	private Connection con;

	public DBDumper(String driver,String connection, String usr, String pass){
		this.driver = driver;
		connectionString = connection;
		user = usr;
		password = pass;
		sqlPaths= new Vector<String>();
		con = null;
	}


	public void addSqlFile(String sqlFile) throws FileNotFoundException{
		File f = new File(sqlFile);

		if(f.exists()){
			sqlPaths.add(sqlFile);
			return;
		}
		else
			throw new FileNotFoundException();
	}

	public void executeFiles() throws SQLException, IOException{
		Statement stm = con.createStatement(); 

		for (String sqlFile : sqlPaths) {
			File f = new File(sqlFile);
			BufferedReader reader = new BufferedReader(new FileReader(f));

			System.out.println(sqlFile);

			String command="Non-Empty";
			while(command.length()>0){
				command="";
				while(command.endsWith("; ")==false){
					String line = reader.readLine();

					if(line==null)
						break;

					line = line.trim();

					if(line.length()==0)
						continue;

					int commentBegin = line.indexOf("--");

					if(commentBegin>0)
						command+= line.substring(0,commentBegin)+" ";
					else if(commentBegin==-1)
						command+= line +" ";
				}

				if(command.length()==0)
					break;
				command = command.substring(0,command.length()-2);
				System.out.println(command);
				stm.executeUpdate(command);

			}
			System.out.println("Atualizacoes realizadas com sucesso!");
		}
		stm.close();
	}

	public void connect() throws ClassNotFoundException, SQLException{
		Class.forName(driver);

		con = DriverManager.getConnection(connectionString, user, password);

	}

	public void disconnect() throws SQLException{
		con.close();
	}

	public Connection initDB() throws ClassNotFoundException, SQLException, IOException {
		this.connect();


		//Cada grupo adiciona seu arquivo sql aqui:
		this.addSqlFile("src/SqlFiles/Diseases/TablesCreate/DiseasesDump.sql");
//		this.addSqlFile("src/SqlFiles/Diseases/TablesCreate/HumanSymptom.sql");
//		this.addSqlFile("src/SqlFiles/Diseases/TablesCreate/DiseaseCausesSymptom.sql");
//		this.addSqlFile("src/SqlFiles/Diseases/TablesCreate/EtiologicalAgent.sql");
//		this.addSqlFile("src/SqlFiles/Geographics/TablesCreate/Biome.sql");
//		this.addSqlFile("src/SqlFiles/Geographics/TablesCreate/Country.sql");
//		this.addSqlFile("src/SqlFiles/Geographics/TablesCreate/CountryIncludesBiome.sql");
//		this.addSqlFile("src/SqlFiles/Geographics/TablesCreate/DiseaseRemainsInBiome.sql");
//		this.addSqlFile("src/SqlFiles/Geographics/TablesCreate/Habitat.sql");
//		this.addSqlFile("src/SqlFiles/Geographics/TablesCreate/HabitatLiesInBiome.sql");
//		this.addSqlFile("src/SqlFiles/Invertebrate/TablesCreate/Body.sql");
//		this.addSqlFile("src/SqlFiles/Invertebrate/TablesCreate/Genus.sql");
//		this.addSqlFile("src/SqlFiles/Invertebrate/TablesCreate/InvertebrateSpecies.sql");
//		this.addSqlFile("src/SqlFiles/Invertebrate/TablesCreate/InvertebrateVectorOfAgent.sql");
//		this.addSqlFile("src/SqlFiles/Common/TablesCreate/User.sql");
//		this.addSqlFile("src/SqlFiles/Common/TablesCreate/AgentCausesDisease.sql");

		try{
			this.executeFiles();
		}
		catch(SQLException e){
			System.out.println(e.getMessage());
			System.out.println("Banco de Dados ja existente, ignorando passo");
		}
		
		return this.con;
	}
}
